#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_greenlet1.py
# author: 马国鑫
# datetime:2022/4/11 21:08
# software: PyCharm

"""
this is function  description 
"""

"""
    使用greenlet实现协程
    1.创建任务
    2.创建greenlet对象
    3.手动switch任务
    帮忙自动调度的微线程
"""
import time
from greenlet import greenlet


def work1():
    while True:
        print('正在执行work1........')
        time.sleep(0.5)
        obj2.switch()  # 切换到第二个任务


def work2():
    while True:
        print('正在执行work2........')
        time.sleep(0.5)
        obj1.switch()  # 切换到第一个任务


if __name__ == '__main__':
    obj1 = greenlet(work1)
    obj2 = greenlet(work2)
    # 先执行第一个任务，只需告诉谁先执行
    obj1.switch()
